package org.bamboo.easyejb.client;

import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * EasyEJBConfigUtil. Use it to getInitialContextProperties.
 *  
 * @author Ramond KK He, raymond.kk.he@gmail.com 
 * @version 1.0 
 * 2008-11-27  
 */
public class EasyEJBConfigUtil {
	private static Hashtable staticInitialContextProperties ; //for remote and remote local invoke
	private static Log log = LogFactory.getLog(EJBConnection.class);
	public static Hashtable getInitialContextProperties() {
		
		if(staticInitialContextProperties!=null)
			return staticInitialContextProperties;
		InputStream is = null;
		Properties ps = null;
		try {
			is = EasyEJBConfigUtil.class.getResourceAsStream("/easyejb.jndi.properties");
			
			ps = new Properties();
			ps.load(is);
			
			try{	
				//test in jboss4.0.0
				//also can be tested with remote WAS, Weblogic etc.
				if(log.isDebugEnabled()) log.debug("initialContextProperties:  " + ps);
				if(ps.getProperty("java.naming.factory.initial")==null)
					throw new IllegalArgumentException("java.naming.factory.initial not set in jndi.properties!");
				if(ps.getProperty("java.naming.provider.url")==null)
					throw new IllegalArgumentException("java.naming.provider.url not set in jndi.properties!");
				
				if(ps.getProperty("ejbNameSeparator")!=null)
					if(log.isDebugEnabled()) log.debug("ejbNameSeparator in config file:[" + ps.getProperty("ejbNameSeparator")+"]");
				
				staticInitialContextProperties = new Hashtable(3);
				staticInitialContextProperties.put("java.naming.factory.initial",ps.getProperty("java.naming.factory.initial"));
				staticInitialContextProperties.put("java.naming.provider.url", ps.getProperty("java.naming.provider.url"));
				
				staticInitialContextProperties.put("ejbNameSeparator", ps.getProperty("ejbNameSeparator"));
				
				
				return staticInitialContextProperties;
				
			} catch (Throwable e) {	
				System.err.println("Initcontext error: " + e);
				e.printStackTrace();
			}
		} catch (Exception e) {	
			
			
			staticInitialContextProperties = new Hashtable(3);
			if(System.getProperty("java.naming.factory.initial")==null || System.getProperty("java.naming.provider.url")== null) {
				System.err.println("[WARN] initialContextProperties not set: \r\n java.naming.factory.initial \r\n java.naming.provider.url  \r\n or use easyejb.jndi.properties or EJBConnection(Hashtable initialContextProperties) constructor! " );
			}else {
				staticInitialContextProperties = new Hashtable();
				staticInitialContextProperties.put("java.naming.factory.initial", System.getProperty("java.naming.factory.initial"));
				staticInitialContextProperties.put("java.naming.provider.url", System.getProperty("java.naming.provider.url"));
			}				
			
			System.err.println("[WARN] easyejb.jndi.properties no found! Your should set jndi context by -D or in code.  " + e);
			
		}
		
		//return null,if fail to init
		return null;		
	}

}
